package com.yc.chaoshi.cs_manage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.chaoshi.cs_manage.service.IUmsResourceService;
import com.yc.chaoshi.cs_manage.service.IUmsRolesResourceService;
import com.yc.chaoshi.entity.UmsResource;
import com.yc.common.api.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xz
 * @since 2022-08-04
 */
@RestController
@RequestMapping("umsResource")
@Slf4j
public class UmsResourceController {

    @Resource
    IUmsResourceService umsResourceService;

    @Resource
    IUmsRolesResourceService rolesResourceService;
    @GetMapping("list")
    public CommonResult list(){
        return CommonResult.success(umsResourceService.list());
    }

    @GetMapping("listByid/{roleid}")
    public CommonResult listByid(@PathVariable Integer roleid){
        return CommonResult.success(umsResourceService.listByid(roleid));
    }
    /**
     * 根据名字查询所有权限
     * @param pageNum
     * @param pageSize
     * @param sreachname
     * @return
     */
    @PostMapping("listAll")
    public CommonResult listAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                @RequestParam(value = "sreachname") String sreachname,
                                @RequestParam(value = "sreachurl") String sreachurl){
        Page page = umsResourceService.listAll(pageNum,pageSize,sreachname,sreachurl);
        return CommonResult.success(page);
    }

    /**
     * 查询所有该权限的资源
     * @param pageNum
     * @param pageSize
     * @param sreachname
     * @param sreachurl
     * @param roleid
     * @return
     */
    @PostMapping("listResource")
    public CommonResult listResource(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                     @RequestParam(value = "sreachname") String sreachname,
                                     @RequestParam(value = "sreachurl") String sreachurl,
                                     @RequestParam(value = "roleid") Integer roleid){
        Page page = umsResourceService.listResource(pageNum,pageSize,sreachname,sreachurl,roleid);
        return CommonResult.success(page);
    }
    /**
     * 删除权限
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Integer id){
        boolean result=umsResourceService.removeById(id);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    /**
     * 编辑权限信息
     * @param umsRoles
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public CommonResult edit(@RequestBody @Validated UmsResource umsRoles){
        Boolean result=umsResourceService.edit(umsRoles);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    /**
     * 添加权限信息
     * @param umsResource
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public CommonResult add(@RequestBody @Validated UmsResource umsResource){
        Boolean result=umsResourceService.add(umsResource);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    @GetMapping("/deleteResource/{resourceid}/{roleid}")
    public CommonResult deleteResource(@PathVariable Integer resourceid,
                                       @PathVariable Integer roleid){
        boolean result=rolesResourceService.delete(resourceid,roleid);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
}

